﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalText
{
    public partial class Means : Form
    {
        private int id;
        
        public Means()
        {
            InitializeComponent();
        }

        public Means(int id, string title, string author)
        {
            InitializeComponent();
            this.id = id;
            textTitle.Text = title;
            textAuthor.Text = author;
        }
        //确认保存
        private void button1_Click(object sender, EventArgs e)
        {
            var title = textTitle.Text;
            var author = textAuthor.Text;

            //更新
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(title))
                {
                    var sql1 = string.Format("update Articles set Title='{0}',Author='{1}'  Where Id={2}",title,author, this.id);
                    var res = DbHelp.AddOrEditOrDelectSave(sql1);
                    if (res == 1)
                    {
                        MessageBox.Show("修改成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //添加
            else
            {
                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("insert into Articles(Title,Author) values ('{0}','{1}')", title,author);

                    var res = DbHelp.AddOrEditOrDelectSave(sql);



                    if (res == 1)
                    {
                        MessageBox.Show("添加成功","提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("信息不为空");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
